﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Framework.Verifications;
using Pixysoft.Weblications.Sango.Environment;

namespace Pixysoft.Weblications.Sango.Dao
{
    class UsrPropertyDao
    {
        public static Entities.Usr_Property createProperty(Entities.Usr_Profile profile)
        {
            Entities.Sys_Famesetting fame = SysFameSettingHelper.getDefaultFame();

            Entities.Usr_Property orm = Entities.Local.Usr_Property;
            orm.Usercode = profile.Usercode;
            orm.Nickname = profile.Nickname;
            orm.Fametitletypecode = fame.Fametitletypecode;
            orm.Fametitlename = fame.Fametitlename;
            PlayerFormular.InitializeProperty(orm);
            return orm;
        }

        public static Entities.Usr_Property getProperty(string usercode)
        {
            Entities.Usr_Property orm = SettingHelper.GetNoebe().EntityNoebeCommand.SelectByPk<Entities.Usr_Property>(usercode);

            if (orm == null)
                throw Exceptions.VerificationFailedException(usercode);

            return orm;
        }


        /// <summary>
        /// 使用仓库
        /// </summary>
        /// <param name="orm"></param>
        /// <returns></returns>
        public static bool enterRepository(Entities.Usr_Property orm)
        {
            if (orm.Usedrepositorycount >= orm.Repositorycount)
                return false;

            orm.Usedrepositorycount = orm.Usedrepositorycount + 1;
            return true;
        }

        /// <summary>
        /// 空闲 仓库
        /// </summary>
        /// <param name="orm"></param>
        public static void leaveRepository(Entities.Usr_Property orm)
        {
            orm.Usedrepositorycount = orm.Usedrepositorycount - 1;

            if (orm.Usedrepositorycount < 0)
                orm.Usedrepositorycount = 0;
        }

        /// <summary>
        /// 购买训练位
        /// </summary>
        /// <param name="orm"></param>
        public static void trainBuy(Entities.Usr_Property orm)
        {
            orm.Trainingcount = orm.Trainingcount + 1;
        }


        /// <summary>
        /// 突飞猛进 如果没有锦旗 则返回false
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        protected static bool upgradeOfficerExtra(Entities.Usr_Property orm)
        {
            if (orm.Bannercount <= 0)
                return false;

            orm.Bannercount = orm.Bannercount - 1;
            return true;
        }

    }
}
